package database;

import java.net.UnknownHostException;
import java.util.ArrayList;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;

public class DatabaseOperations implements IDatabaseOperations{

	private MongoClient mongoClient;
	private DB db;
	private DBObject jobsMap;
	private DBCollection dbCollection;
	private DBObject record;

	public DatabaseOperations(){
		try {
			mongoClient = new MongoClient( "localhost" , 27017 );
			db = mongoClient.getDB( "RoadRunner" );
			dbCollection = db.getCollection("JOBS");          
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	@Override
	public void addJob(int orderId, String expName, String expAddress, String destName, String destAddress, String transporterName) {
		jobsMap = new BasicDBObject();
		jobsMap.put(CollectionKey.ORDER_ID.toString(), orderId);
		jobsMap.put(CollectionKey.EXP_NAME.toString(), expName);
		jobsMap.put(CollectionKey.EXP_ADDRESS.toString(), expAddress);
		jobsMap.put(CollectionKey.DEST_NAME.toString(), destName);
		jobsMap.put(CollectionKey.DEST_ADDRESS.toString(), destAddress);
		jobsMap.put(CollectionKey.TRANSPORTER_NAME.toString(), transporterName);
		jobsMap.put(CollectionKey.JOB_STATE.toString(), JobState.NOT_ASSIGNED.toString());
		dbCollection.save(jobsMap);

	}

	@Override
	public String getJob(int orderId) {
		record = new BasicDBObject();
		String result = "";
		BasicDBObject query = new BasicDBObject(CollectionKey.ORDER_ID.toString(), orderId);

		DBCursor cursor = dbCollection.find(query);
		try {
			if(cursor.hasNext()){
				record = cursor.next();
				record.removeField("_id");
				result = record.toString();
				System.out.println(result);
			}
		} finally {
			   cursor.close();
			}
		
//		try {
//		   while(cursor.hasNext()) {
//		       
//		   }
//		} finally {
//		   cursor.close();
//		}
		
		return result;
	}
	
	@Override
	public ArrayList<String> getAllJobs() {
		ArrayList<String> results = new ArrayList<String>();
		DBCursor cursor = dbCollection.find();
		
		try {
		   while(cursor.hasNext()) {
		       record = cursor.next();
		       
		       record.removeField("_id");
		       results.add(record.toString());
		       
		   }
		} finally {
		   cursor.close();
		}
		
		for(String s: results){
			System.out.println(s);
		}
		
		return results;
	}


	public void deleteJob(int orderId) {
		record = new BasicDBObject();
		BasicDBObject query = new BasicDBObject(CollectionKey.ORDER_ID.toString(), orderId);
		DBCursor cursor = dbCollection.find(query);
		try {
			if(cursor.hasNext()){
				record = cursor.next();
				dbCollection.remove(record);
				System.out.println("Removed: "+ record);
			}
		} finally {
			   cursor.close();
			   record = null;
		}
	}

	@Override
	public void editJob(int orderId, String newExpName, String newExpAddress,
			String newDestName, String newDestAddress, String transporterName,
			String jobState) {
		jobsMap = new BasicDBObject();
		jobsMap.put(CollectionKey.ORDER_ID.toString(), orderId);
		jobsMap.put(CollectionKey.EXP_NAME.toString(), newExpName);
		jobsMap.put(CollectionKey.EXP_ADDRESS.toString(), newExpAddress);
		jobsMap.put(CollectionKey.DEST_NAME.toString(), newDestName);
		jobsMap.put(CollectionKey.DEST_ADDRESS.toString(), newDestAddress);
		jobsMap.put(CollectionKey.TRANSPORTER_NAME.toString(), transporterName);
		jobsMap.put(CollectionKey.JOB_STATE.toString(), jobState);
	 
		BasicDBObject searchQuery = new BasicDBObject().append(CollectionKey.ORDER_ID.toString(), orderId);
	 
		dbCollection.update(searchQuery, jobsMap);
	}
	
	@Override
	public void editJobState(int orderId, String jobState){
		BasicDBObject updateObject = new BasicDBObject();
		updateObject.append("$set", new BasicDBObject().append(CollectionKey.JOB_STATE.toString(), jobState));
	 
		BasicDBObject searchQuery = new BasicDBObject().append(CollectionKey.ORDER_ID.toString(), orderId);
	 
		dbCollection.update(searchQuery, updateObject);


	}
	
	@Override
	public void closeConnection(){
		mongoClient.close();
	}
	
}
